﻿using System;
using System.Windows.Forms;
using BusinessServices;
using DataEntities;

namespace CarLeaseManageSystem
{
    public partial class FormEntry : Form
    {
        private readonly SettingService _settingService;
        private readonly Setting _setting;

        public FormEntry()
        {
            _settingService = new SettingService();
            _setting = _settingService.Get();
            InitializeComponent();
        }

        private void FormEntry_Load(object sender, EventArgs e)
        {
            if (_setting.bRembPwd)
            {
                txtBoxPwd.Text = _setting.PwdBackup;
                txtBoxPwd.Enabled = false;
                chkBoxRemb.Checked = true;
            }
        }

        private void Unlock(object sender, EventArgs e)
        {
            if (((CheckBox)sender).Checked == false)
            {
                txtBoxPwd.Enabled = true;
            }
        }

        private void Login(object sender, EventArgs e)
        {
            if (_settingService.ValidatePwd(txtBoxPwd.Text))
            {
                _settingService.Update(s =>
                {
                    s.bRembPwd = chkBoxRemb.Checked;
                    s.PwdBackup = txtBoxPwd.Text;
                });
                FormMain mainForm = new FormMain();
                mainForm.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("密码错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool _isLogin = true;
        private double _iOpac;

        private void FadeOut(object sender, EventArgs e)
        {
            timerEntry.Enabled = true;
            _isLogin = false;
            _iOpac = 100;
        }

        private void FadeIn(object sender, EventArgs e)
        {
            if (_isLogin)
            {
                if (_iOpac > 100)
                    timerEntry.Enabled = false;
                else
                {
                    this.Opacity = _iOpac / 100;
                    _iOpac++;
                }
            }
            else
            {
                if (_iOpac < 0)
                {
                    this.Close();
                }
                else
                {
                    this.Opacity = _iOpac / 100;
                    _iOpac--;
                }
            }
        }
    }
}
